package com.uqpsoftware.uqplist.tasks;

import java.util.List;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.uqpsoftware.uqplist.R;
import com.uqpsoftware.uqplist.Util;
import com.uqpsoftware.uqplist.tabs.ListData;
import com.uqpsoftware.uqplist.tabs.RelayClickInfoToFragmentListener;

public class TaskListAdapter extends ArrayAdapter<Task> {

	private List<Task> mTasks;
	private Context mContext;
	private ListData mListData;

	public TaskListAdapter(Context context, int textViewResourceId,
			List<Task> tasks) {
		super(context, textViewResourceId, tasks);
		mTasks = tasks;
		mContext = context;
	}

	public void setListData(ListData listData) {
		mListData = listData;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View row = convertView;
		LayoutInflater inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		// row = inflater.inflate(R.layout.task, parent, false);
		row = inflater.inflate(R.layout.task_checkbox_and_textview, parent,
				false);
		CheckBox checkBox = (CheckBox) row.findViewById(R.id.taskCheckBox);
		TextView textView = (TextView) row.findViewById(R.id.taskTextView);
		Task task = mTasks.get(position);
		if (task.isCompleted()) {
			textView.setPaintFlags(textView.getPaintFlags()
					| Paint.STRIKE_THRU_TEXT_FLAG);
			checkBox.setChecked(true);
			checkBox.setPaintFlags(checkBox.getPaintFlags()
					| Paint.STRIKE_THRU_TEXT_FLAG);
		} else {
			checkBox.setPaintFlags(checkBox.getPaintFlags()
					& (~Paint.STRIKE_THRU_TEXT_FLAG));
			textView.setPaintFlags(textView.getPaintFlags()
					& (~Paint.STRIKE_THRU_TEXT_FLAG));
		}
		// checkBox.setText(mTasks.get(position).getName());
		textView.setText(mTasks.get(position).getName());
		if (Util.DEBUG) {
			textView.setText(mTasks.get(position).getName() + " "
					+ mTasks.get(position).getID());
		}
		RelayClickInfoToFragmentListener sendCheckedDataToFragment = new RelayClickInfoToFragmentListener(
				mTasks.get(position).getID(), mListData);
		textView.setOnClickListener(sendCheckedDataToFragment);
		textView.setOnLongClickListener(sendCheckedDataToFragment);
		checkBox.setOnCheckedChangeListener(sendCheckedDataToFragment);
		checkBox.setOnLongClickListener(sendCheckedDataToFragment);

		return row;
	}

	@Override
	public int getCount() {
		return mTasks.size();
	}

}
